package lt.ntec.danblog.server.blog.model.objectify;

import com.google.inject.Inject;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;
import java.util.List;
import lt.ntec.danblog.server.core.model.Comment;
import lt.ntec.danblog.server.core.model.CommentDAO;
import lt.ntec.danblog.server.core.model.Commentable;
import lt.ntec.danblog.server.core.model.objectify.ObjectifyBaseDAO;

class ObjectifyCommentDAO extends ObjectifyBaseDAO<Comment> implements
        CommentDAO {

    static {
        ObjectifyService.register(BlogComment.class);
    }

    @Inject
    public ObjectifyCommentDAO(Objectify ofy) {
        super(ofy);
    }

    @Override
    public Class<? extends Comment> getEntityClass() {
        return BlogComment.class;
    }

    @Override
    public List<? extends Comment> getListByParent(Commentable commentable) {
        Key<Commentable> key =
                new Key<Commentable>(commentable.getClass(), commentable.getId());
        Query<BlogComment> filter =
                ofy.query(BlogComment.class).
                filter("parentKey", key);
        List<BlogComment> list = filter.list();


        return list;
    }

    @Override
    public void save(List<? extends Comment> comments) {
        ofy.put(comments);
    }

}
